import History from './base'

export default class HashHistory extends History {
  constructor (router) {
    super(router)
    // 1 确保首次访问的路径是 #/
    ensureSlash()
  }

  getCurrentLocation () {
    return window.location.hash.slice(1)
  }

  setUpListener () {
    window.addEventListener('hashchange', () => {
      this.transitionTo(this.getCurrentLocation())
    })
  }
}

function ensureSlash () {
  if (window.location.hash) {
    return
  }
  window.location.hash = '/'
}
